package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.interfaces.PopResult;
import com.sgb.kjwl.model.entity.PickSelectEntity2;
import com.sgb.kjwl.model.entity.TwoLevelEntity;

import java.util.List;


public class ScreenTextViewForTwoLevel<D extends PickSelectEntity2> extends RelativeLayout {

    private Context context;
    private LinearLayout ll_content;
    private TextView tv_content;
    private ImageView img;
    private TwoLevelPopWindow<D> basePopWindow;
    private String name;
    private int type;
    private PopResult mPopResult;

    public void setmPopResult(PopResult mPopResult) {
        this.mPopResult = mPopResult;
    }

    public ScreenTextViewForTwoLevel(Context context) {
        this(context, null);
    }

    public ScreenTextViewForTwoLevel(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScreenTextViewForTwoLevel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init(attrs,context);
    }

    private void init(AttributeSet attrs,Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_item_screen_text_for_circle, this);
        ll_content = view.findViewById(R.id.ll_content);
        tv_content = view.findViewById(R.id.tv_content);
        img = view.findViewById(R.id.img);

        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ScreenTextViewYoung,0,0);

            try {
                String content = typedArray.getString(R.styleable.ScreenTextViewYoung_textContent);
                if (content != null) {
                    tv_content.setText(content);
                }else {
                    tv_content.setText("");
                }

                int color = typedArray.getColor(R.styleable.ScreenTextViewYoung_textColor, getResources().getColor(R.color.color_333333));
                tv_content.setTextColor(color);

                int background = typedArray.getColor(R.styleable.ScreenTextViewYoung_backgroundColor,0);

                basePopWindow = new TwoLevelPopWindow<>(context);

                basePopWindow.setBackGround(background);

                type = typedArray.getInt(R.styleable.ScreenTextViewYoung_typeForPop,0);
                if (type == 0){
                    ll_content.setBackgroundResource(R.color.white);
                }else {
                    ll_content.setBackgroundResource(R.drawable.search_bg);
                }
            }catch (Exception e){
                e.printStackTrace();
            } finally{
                typedArray.recycle();
            }

        }

        this.setOnClickListener(v -> {
            contentSelect(false,true);
            basePopWindow.showPopwindow(this,0,0);
        });

        basePopWindow.setOnResultClick((key1, key2, key3) -> {
            name = (String) key3;
            if (key2 == null){
                mPopResult.result(name,0,key1);
            }else {
                mPopResult.result(name,0,key1,key2);
            }
        });

        basePopWindow.setOnDisMissClick(() -> {
            if (!name.equals("不限")) {
                contentSelect(false, false);
            }else {
                contentSelect(true,false);
            }
        });
    }

    public void contentSelect(boolean haveContent, boolean isOpen) {
        if (!haveContent && isOpen) {
            //点击没选择 打开了弹窗
            img.setImageDrawable(context.getResources().getDrawable(R.mipmap.pick_select_up_normal));
            tv_content.setTextColor(context.getResources().getColor(R.color.color_5792FD));
        } else if (haveContent && isOpen) {
            //点击已选择的按钮
            img.setImageDrawable(context.getResources().getDrawable(R.mipmap.pick_select_up_normal));
            tv_content.setTextColor(context.getResources().getColor(R.color.color_5792FD));
            if (type == 0){
                return;
            }
            ll_content.setBackground(context.getResources().getDrawable(R.drawable.search_new_blue_bg));
        } else if (haveContent) {
            //点击 选择或重新选择 关闭弹窗
            img.setImageDrawable(context.getResources().getDrawable(R.mipmap.icon_down_red));
            if (type == 0){
                return;
            }
            ll_content.setBackground(context.getResources().getDrawable(R.drawable.search_new_blue_bg));
        } else {
            //取消选择 选择不限或全国
            img.setImageDrawable(context.getResources().getDrawable(R.mipmap.ic_down_arrow_n));
            tv_content.setTextColor(context.getResources().getColor(R.color.color_333333));
            if (type == 0){
                return;
            }
            ll_content.setBackground(context.getResources().getDrawable(R.drawable.search_bg));
        }
    }

    public void setPopWindowData(List<TwoLevelEntity<List<D>>> list){
        basePopWindow.setData(list);
    }

}